//
//  ZXZUtil.m
//  zuxianzhi
//
//  Created by tyu on 28.5.24.
//

#import "ZXZUtil.h"

@implementation ZXZUtil


+ (UIViewController *)createControllerById:(NSString *)Id
                             storyboadName:(NSString *)storyboadName
                                    bundle:(NSBundle *)bundle
{
    bundle = bundle ? bundle : [NSBundle mainBundle];
    storyboadName = storyboadName ? storyboadName : @"Main";
    UIViewController *vc = [[UIStoryboard storyboardWithName:storyboadName bundle:bundle] instantiateViewControllerWithIdentifier:Id];
    return vc;
}



+ (UIWindow *)getKeyWindow
{
    UIWindow* window = nil;
     
    if (@available(iOS 13.0, *))
    {
        for (UIWindowScene* windowScene in [UIApplication sharedApplication].connectedScenes)
        {
            if (windowScene.activationState == UISceneActivationStateForegroundActive || windowScene.activationState == UISceneActivationStateForegroundInactive || windowScene.activationState == UISceneActivationStateBackground)
            {
                window = windowScene.windows.firstObject;
                break;
            }
        }
    } else {
        window = [UIApplication sharedApplication].keyWindow;
    }
    return window;
}



+ (UIImage *)appIconImage
{
    NSArray *icons = [NSBundle mainBundle].infoDictionary[@"CFBundleIcons"][@"CFBundlePrimaryIcon"][@"CFBundleIconFiles"];
    UIImage *image = [UIImage imageNamed:[icons firstObject]];
    return image;
}

+ (NSString *)getAppNameAndVersion
{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];// 获取app版本信息
    NSString *appCurName = [infoDictionary objectForKey:@"CFBundleDisplayName"];
    NSString *appCurVersionNum = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    NSString *appCurBuildNum = [infoDictionary objectForKey:@"CFBundleVersion"];
    return [NSString stringWithFormat:@"%@V%@.%@",appCurName,appCurVersionNum,appCurBuildNum];
}

+ (UIImage *)sniptImageWithRect:(CGRect)rect
                           view:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

+ (NSString *)getAppVersion
{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *appCurVersionNum = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    NSString *appCurBuildNum = [infoDictionary objectForKey:@"CFBundleVersion"];
    return [NSString stringWithFormat:@"%@.%@",appCurVersionNum,appCurBuildNum];
}


@end
